package com.bookdresser.activities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.bookdresser.R;
import com.bookdresser.adapters.IconPagerAdapter;
import com.bookdresser.utils.DensityUtil;
import com.bookdresser.widget.ActionBar;
import com.bookdresser.widget.ActionBar.AbstractAction;
import com.bookdresser.widget.ActionBar.Action;
import com.bookdresser.widget.ActionBar.IntentAction;
import com.bookdresser.widget.TabPageIndicator;

public class TabsActivity extends FragmentActivity {
	private static final String[] CONTENT = new String[] { "筛选", "列表", "发布" };
	private static final int[] ICONS = new int[] {
			R.drawable.perm_group_calendar, R.drawable.perm_group_camera,
			R.drawable.perm_group_device_alarms };

	private PopupWindow mPopupWindow;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_tabs);

		final ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar);
		actionBar.setHomeLogo(R.drawable.logo);
		actionBar.setTitle("书装台");

		final Action shareAction = new IntentAction(this, createShareIntent(),
				R.drawable.ic_title_share_default);
		actionBar.addAction(shareAction);

		actionBar.addAction(new SetttingAction());

		FragmentPagerAdapter adapter = new GoogleMusicAdapter(
				getSupportFragmentManager());

		ViewPager pager = (ViewPager) findViewById(R.id.pager);
		pager.setAdapter(adapter);

		TabPageIndicator indicator = (TabPageIndicator) findViewById(R.id.indicator);
		indicator.setViewPager(pager);

		mPopupWindow = makePopupWindow(TabsActivity.this);
	}

	private Intent createShareIntent() {
		final Intent intent = new Intent(Intent.ACTION_SEND);
		intent.setType("text/plain");
		intent.putExtra(Intent.EXTRA_TEXT, "Shared from the ActionBar widget.");
		return Intent.createChooser(intent, "Share");
	}

	private class SetttingAction extends AbstractAction {

		public SetttingAction() {
			super(R.drawable.ic_title_settings_default);
		}

		@Override
		public void performAction(View view) {
			showPopWindow();
		}
	}

	class GoogleMusicAdapter extends FragmentPagerAdapter implements
			IconPagerAdapter {

		public GoogleMusicAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			switch (position) {
			case 0:
				return ListFragment.newInstance(TabsActivity.this);
			case 1:
				return ListFragment.newInstance(TabsActivity.this);
			case 2:
				return PostFragment.newInstance(TabsActivity.this);
			default:
				break;
			}
			return null;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return CONTENT[position % CONTENT.length].toUpperCase();
		}

		@Override
		public int getIconResId(int index) {
			return ICONS[index];
		}

		@Override
		public int getCount() {
			return CONTENT.length;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add("menu");// 必须创建一项
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onMenuOpened(int featureId, Menu menu) {
		if (mPopupWindow != null) {
			if (!mPopupWindow.isShowing()) {
				showPopWindow();
			}
		}
		return false;// 返回为true 则显示系统menu
	}

	private void showPopWindow() {
		/* 最重要的一步：弹出显示 在指定的位置(parent) 最后两个参数 是相对于 x / y 轴的坐标 */
		mPopupWindow.showAtLocation(findViewById(R.id.actionbar), Gravity.TOP
				| Gravity.RIGHT, 15, DensityUtil.dip2px(TabsActivity.this, 75));
	}

	// 创建一个包含自定义view的PopupWindow
	private PopupWindow makePopupWindow(Context context) {
		PopupWindow window;
		window = new PopupWindow(context);

		View contentView = LayoutInflater.from(this).inflate(R.layout.menu,
				null);
		window.setContentView(contentView);

		contentView.setFocusableInTouchMode(true);
		contentView.setOnKeyListener(new OnKeyListener() {

			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if ((keyCode == KeyEvent.KEYCODE_MENU)
						&& (mPopupWindow.isShowing())) {
					mPopupWindow.dismiss();// 这里写明模拟menu的PopupWindow退出就行
					return true;
				}
				return false;
			}
		});

		TextView tv_menu_login = (TextView) contentView
				.findViewById(R.id.tv_menu_login);
		tv_menu_login.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Toast.makeText(TabsActivity.this, "login", Toast.LENGTH_LONG)
						.show();
				mPopupWindow.dismiss();
			}
		});

		TextView tv_menu_setting = (TextView) contentView
				.findViewById(R.id.tv_menu_setting);
		tv_menu_setting.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Toast.makeText(TabsActivity.this, "setting", Toast.LENGTH_LONG)
						.show();
				mPopupWindow.dismiss();
			}
		});

		window.setWidth(DensityUtil.dip2px(context, 200));
		window.setHeight(LayoutParams.WRAP_CONTENT);

		// 设置PopupWindow外部区域是否可触摸
		window.setFocusable(true); // 设置PopupWindow可获得焦点
		window.setTouchable(true); // 设置PopupWindow可触摸
		window.setOutsideTouchable(true); // 设置非PopupWindow区域可触摸
		return window;
	}
}
